package com.cade.agriculturemall.view.main;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.avos.avoscloud.AVUser;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.base.IBasePresenter;
import com.cade.agriculturemall.model.UserInfoEntity;
import com.cade.agriculturemall.utils.VerifyUtil;
import com.cade.agriculturemall.view.home.HomeFragment;
import com.cade.agriculturemall.view.classify.ClassifyFragment;
import com.cade.agriculturemall.view.login.LoginActivity;
import com.cade.agriculturemall.view.personalCenter.PersonalCenterFragment;
import com.cade.agriculturemall.view.shoppingCar.ShoppingCarFragment;

public class MainActivity extends BaseActivity {
    private FrameLayout mFrameLayout;
    private BottomNavigationBar mBottomNavigationBar;
    private android.support.v4.app.FragmentManager fm;
    private HomeFragment homeFragment;
    private ClassifyFragment classifyFragment;
    private PersonalCenterFragment personalCenterFragment;
    private ShoppingCarFragment shoppingCardFragment;
    private int curPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void setupView() {
        mFrameLayout = (FrameLayout) findViewById(R.id.main_framelayout);
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.main_bottomnavigationbar);
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.home, "首页"))
                .addItem(new BottomNavigationItem(R.mipmap.classify, "分类"))
                .addItem(new BottomNavigationItem(R.mipmap.shopping_cart, "购物车"))
                .addItem(new BottomNavigationItem(R.mipmap.user, "个人"))
                //不触发事件
                .setFirstSelectedPosition(0)
                //进行渲染，所有设置属性后都需要调用该方法
                .initialise();
    }

    @Override
    public void setupEvent() {
         /*
        * 设置模式
        * MODE_DEFAULT:
        * 如果Item的个数<=3就会使用MODE_FIXED模式，否则使用MODE_SHIFTING模式
        *
        * MODE_FIXED
        * 填充模式，未选中的Item会显示文字，没有换挡动画。
        *
        * MODE_SHIFTING
        * 换挡模式，未选中的Item不会显示文字，选中的会显示文字。在切换的时候会有一个像换挡的动画
        */
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_CLASSIC);

        /*
        * 设置背景样式
        * BACKGROUND_STYLE_DEFAULT：
        * 如果设置的Mode为MODE_FIXED，将使用BACKGROUND_STYLE_STATIC 。如果Mode为MODE_SHIFTING将使用BACKGROUND_STYLE_RIPPLE
        *
        * BACKGROUND_STYLE_STATIC
        * 点击的时候没有水波纹效果
        *
        * BACKGROUND_STYLE_RIPPLE
        * 点击的时候有水波纹效果
        */
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                curPosition = position;
                FragmentTransaction ft = fm.beginTransaction();
                hideFragment(ft);

                switch (position) {
                    case 0:
                        if (homeFragment == null) {
                            homeFragment = (HomeFragment) Fragment.instantiate(getApplicationContext(), HomeFragment.class.getName());
                            ft.add(R.id.main_framelayout, homeFragment);
                        } else {
                            ft.show(homeFragment);
                        }
                        break;
                    case 1:
                        if (classifyFragment == null) {
                            classifyFragment = (ClassifyFragment) Fragment.instantiate(getApplicationContext(), ClassifyFragment.class.getName());
                            ft.add(R.id.main_framelayout, classifyFragment);
                        } else {
                            ft.show(classifyFragment);
                        }
                        break;
                    case 2:
                        if (shoppingCardFragment == null) {
                            if (!VerifyUtil.hadLogin()) {
                                showDialog();
                            } else {
                                shoppingCardFragment = (ShoppingCarFragment) Fragment.instantiate(getApplicationContext(), ShoppingCarFragment.class.getName());
                                ft.add(R.id.main_framelayout, shoppingCardFragment);
                            }
                        } else {
                            if (!VerifyUtil.hadLogin()) {
                                showDialog();
                            } else {
                                ft.show(shoppingCardFragment);
                            }
                        }
                        break;
                    case 3:
                        if (personalCenterFragment == null) {
                            if (!VerifyUtil.hadLogin()) {
                                showDialog();
                            } else {
                                personalCenterFragment = new PersonalCenterFragment();
                                ft.add(R.id.main_framelayout, personalCenterFragment);
                                UserInfoEntity mEntity = loadPensonalData();
                                personalCenterFragment.getInfo(mEntity);
                            }
                        } else {
                            if (!VerifyUtil.hadLogin()) {
                                showDialog();
                            } else {
                                ft.show(personalCenterFragment);
                            }
                        }
                        break;
                }
                ft.commit();
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

    }

    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.system_message).setMessage(R.string.have_no_login).
                setPositiveButton("登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                        intent.putExtra("goBack",true);
                        startActivity(intent);
                    }
                }).setNegativeButton("稍后去登陆", null);
        builder.show();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (classifyFragment != null) {
            ft.hide(classifyFragment);
        }
        if (personalCenterFragment != null) {
            ft.hide(personalCenterFragment);
        }
        if (shoppingCardFragment != null) {
            ft.hide(shoppingCardFragment);
        }
    }

    @Override
    public void setupData() {
        setDefaultFragment();
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        fm = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
        homeFragment = (HomeFragment) Fragment.instantiate(this, HomeFragment.class.getName());
        transaction.add(R.id.main_framelayout, homeFragment);
        transaction.commit();
    }


    @Override
    public IBasePresenter setupPresenter() {
        return null;
    }

    public UserInfoEntity loadPensonalData() {
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
        return user;

    }
}
